package com.example.host.utils;

import com.alibaba.fastjson.JSONObject;
import com.example.host.domain.SessionInfo;
import org.springframework.stereotype.Component;

@Component
public class SessionInfoUtils {
    public void insertOrReplaceSessionInfo(JSONObject sessionInfo){
        boolean exist = false;
        for (int i = 0; i < SessionInfo.sessionList.size(); i++) {
            if (SessionInfo.sessionList.get(i).getString("deviceClientId").equals(sessionInfo.getString("deviceClientId"))){
                exist = true;
                SessionInfo.sessionList.get(i).put("address",sessionInfo.getString("address"));
                SessionInfo.sessionList.get(i).put("cid",sessionInfo.getString("cid"));
                SessionInfo.sessionList.get(i).put("msg",sessionInfo.getString("msg"));
                break;
            }
        }
        if (!exist){
            SessionInfo.sessionList.add(sessionInfo);
        }
    }
    
    public void deleteSessionInfo(String deviceClientId){
        for (int i = 0; i < SessionInfo.sessionList.size(); i++) {
            if (SessionInfo.sessionList.get(i).getString("deviceClientId").equals(deviceClientId)){
                SessionInfo.sessionList.remove(i);
                break;
            }
        }
    }
    
    public JSONObject getSessionInfo(String deviceClientId){
        for (int i = 0; i < SessionInfo.sessionList.size(); i++) {
            if (SessionInfo.sessionList.get(i).getString("deviceClientId").equals(deviceClientId)){
                return SessionInfo.sessionList.get(i);
            }
        }
        return null;
    }
    
    public void updateMsg(JSONObject sessionInfo) {
        for (int i = 0; i < SessionInfo.sessionList.size(); i++) {
            if (SessionInfo.sessionList.get(i).getString("deviceClientId").equals(sessionInfo.getString("deviceClientId"))) {
                SessionInfo.sessionList.get(i).put("msg",sessionInfo.getString("msg"));
                break;
            }
        }
    }
    
    public boolean isInstallSuccess(String deviceClientId){
        for (int i = 0; i < SessionInfo.sessionList.size(); i++) {
            if (SessionInfo.sessionList.get(i).getString("deviceClientId").equals(deviceClientId)){
                return true;
            }
        }
        return false;
    }
    
    public boolean isUninstallSuccess(String deviceClientId){
        for (int i = 0; i < SessionInfo.sessionList.size(); i++) {
            if (SessionInfo.sessionList.get(i).getString("deviceClientId").equals(deviceClientId)){
                if (SessionInfo.sessionList.get(i).getString("msg").contains("远控服务注销成功！")){
                    return true;
                }
                break;
            }
        }
        return false;
    }
}
